import axios from "axios";
import type { AxiosInstance } from "axios";
import { normalizeError } from "./normalizeError";

const service: AxiosInstance = axios.create({
  baseURL: '/api', // 确保所有请求通过 Vite 代理转发到 8080
  timeout: 30000,
  withCredentials: true,
  headers: {
    "Content-Type": "application/json",
  },
});

// 统一错误规范化，其余逻辑在上层封装（request.ts）处理
service.interceptors.request.use(
  (config) => config,
  (err) => Promise.reject(normalizeError(err)),
);

service.interceptors.response.use(
  (resp) => resp,
  (err) => Promise.reject(normalizeError(err)),
);

export default service;
